package org.arch_learn.zookeeper.curator_client_api;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class CreateNode {

    public static void main(String[] args) throws Exception {
        //不使用fluent编程风格
        ExponentialBackoffRetry exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        //使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(3000)
                .retryPolicy(exponentialBackoffRetry)
                //设置了一个独立的命名空间，有利于实现不同zookeeper业务的隔离
                .namespace("base")
                .build();
        client.start();
        System.out.println("会话被创建了。。。。");

        //创建节点
        String path = "/curator-node/c1";

        String forPath = client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT).forPath(path, "123".getBytes());
        System.out.println("节点递归创建成功" + forPath);
    }
}
